package org.zjx.app.action.gather;


import org.zjx.analysis.analyze.window.impl.RoleAnalysis;
import org.zjx.app.action.AbstractWindowAction;
import org.zjx.command.state.RoleState;
import org.zjx.window.window.IWindow;

import java.awt.*;
import java.awt.event.KeyEvent;

public class GatherRoleAction extends AbstractWindowAction {
    private RoleAnalysis roleAnalysis;

    public GatherRoleAction(IWindow targetWindow) {
        super(targetWindow,true);
        analysis();
    }

    private void analysis() {
        roleAnalysis = new RoleAnalysis(targetWindow);
    }

    @Override
    public void open() {
        mouseAloofWindow(roleAnalysis.windowArea == null? roleAnalysis.getOriginBounds(): roleAnalysis.windowArea);
        if (roleAnalysis.isOpened()) return;
        targetWindow.actions().hotkey(KeyEvent.VK_ALT, KeyEvent.VK_W).delay(500);
        targetWindow.flushScreenshot();
        analysis();
        open();
    }

    @Override
    public void close() {
        analysis();
        if (!roleAnalysis.isOpened()) return;
        targetWindow.actions().hotkey(KeyEvent.VK_ALT, KeyEvent.VK_W).delay(500);
        targetWindow.flushScreenshot();
        mouseAloofWindow(roleAnalysis.windowArea == null? roleAnalysis.getOriginBounds(): roleAnalysis.windowArea);
        close();
    }

    @Override
    public void reSetWindow() {
        if (roleAnalysis.isTransborder()) {
            Rectangle from = roleAnalysis.conversion(roleAnalysis.windowArea,roleAnalysis.getRefImageArea());
            Rectangle to = roleAnalysis.conversion(roleAnalysis.getOriginBounds(),roleAnalysis.getRefImageArea());
            targetWindow.actions().drag(from.x, from.y, to.x, to.y).delay(100);
            mouseAloofWindow(roleAnalysis.getOriginBounds());
            targetWindow.flushScreenshot();
            analysis();
        }
    }

    public void mouseAloofWindow(Rectangle rect) {
        if (roleAnalysis.mouseInRect(rect)) {
            Point safePoint = roleAnalysis.findLargestRemainingAreaCenter(rect);
            targetWindow.actions().gameMoveTo(safePoint.x, safePoint.y).delay(200);
            targetWindow.flushScreenshot();
            analysis();
        }
    }

    @Override
    protected void doExecute() throws Exception {
        open();
        reSetWindow();
        RoleState roleState = new RoleState();
        stateManager.registerState(roleState);

        roleState.setLevel(roleAnalysis.getLevel());
        roleState.setVitality(roleAnalysis.getVitality());
        roleState.setMaxVitality(roleAnalysis.getMaxVitality());
        roleState.setPhysical(roleAnalysis.getPhysical());
        roleState.setMaxPhysical(roleAnalysis.getMaxPhysical());
        roleState.setPopularity(roleAnalysis.getPopularity());
        roleState.setGangContribution(roleAnalysis.getGangContribution());
        roleState.setSectContribution(roleAnalysis.getSectContribution());
        roleState.setPotential(roleAnalysis.getPotential());
        roleState.setHealth(roleAnalysis.getHealth());
        roleState.setMaxHealth(roleAnalysis.getMaxHealth());
        roleState.setMana(roleAnalysis.getMana());
        roleState.setMaxMana(roleAnalysis.getMaxMana());
        roleState.setRage(roleAnalysis.getRage());
        roleState.setMaxRage(roleAnalysis.getMaxRage());
        roleState.setInjury(roleAnalysis.getInjury());


        close();
    }
}
